//
//  WBStatusCell.swift
//  sinaWeibo
//
//  Created by 吴熠 on 16/11/16.
//  Copyright © 2016年 吴熠. All rights reserved.
//

import UIKit
import SDWebImage

private let margin: CGFloat = 8

private let itemMargin : CGFloat = 2
private let maxWidth = UIScreen.main.bounds.width-2*margin
private let itemWidth = (maxWidth-2*itemMargin)/3

class WBStatusCell: UITableViewCell {
    //微博作者头像
    @IBOutlet weak var iconView: UIImageView!
    //作者昵称
    @IBOutlet weak var userName: UILabel!
    //会员等级
    @IBOutlet weak var vipGrade: UIImageView!
    //微博发送时间
    @IBOutlet weak var time: UILabel!
    //微博来源
    @IBOutlet weak var source: UILabel!
    //微博正文
    @IBOutlet weak var context: UILabel!
    
    @IBOutlet weak var Vuser: UIImageView!
    
    //微博配图视图
    
    @IBOutlet weak var pictureView: WBPictureView!
    
    @IBOutlet weak var pictureViewHeight: NSLayoutConstraint!
    
    @IBOutlet weak var pictureViewWidth: NSLayoutConstraint!
    
    //流水布局
    
    @IBOutlet weak var flowLayOut: UICollectionViewFlowLayout!
    
    //底部工具栏
    
    @IBOutlet weak var toolBar: UIView!
    
    @IBOutlet weak var retweetedLabel: UILabel!
    //配图视图顶部约束
    @IBOutlet weak var pictureTopCons: NSLayoutConstraint!
    
    
    var viewModel : WBStatusViewModel? {
        didSet{
            //设置子控件数据
            iconView.sd_setImage(with: viewModel?.iconURL)
            time.text = viewModel?.timeText
            source.text = viewModel?.sourceText
            context.text = viewModel?.status?.text
            vipGrade.image = viewModel?.mbRankImage
            Vuser.image = viewModel?.verifitedImage
            userName.text = viewModel?.status?.user?.name
            //图片数
            let picCount = viewModel?.imgUrls?.count ?? 0
            
            //根据图片的count修改顶部约束对象的值
            pictureTopCons.constant = (picCount == 0 ? 0 : margin)
            let pictureViewSize = calculatePictureViewSize(count: picCount)
            
            //重新设置pictureView的约束
            pictureViewHeight.constant = pictureViewSize.pSize.height
            pictureViewWidth.constant = pictureViewSize.pSize.width
            //再更新item的约束
            flowLayOut.itemSize = pictureViewSize.itemSize
            
            //拿到转发微博的东西都要先经过viewMode.status.retweeted_status的属性
            //设置配图视图
            pictureView.pictureInfo = viewModel?.imgUrls
            
            pictureView.layoutIfNeeded()
            
            //转发微博的文本
            retweetedLabel?.text = viewModel?.status?.retweeted_status?.text
            retweetedLabel?.numberOfLines = 0
            retweetedLabel?.preferredMaxLayoutWidth = UIScreen.main.bounds.width
            
            pictureView.pictureInfo = viewModel?.imgUrls
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置背景颜色
//        self.contentView.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
        //设置行数0
        self.context.numberOfLines = 0
        //最大布局宽度
        self.context.preferredMaxLayoutWidth = UIScreen.main.bounds.width-2*margin
        
        //设置流水布局
        flowLayOut.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayOut.minimumLineSpacing = itemMargin
        flowLayOut.minimumInteritemSpacing = itemMargin
        
        layoutIfNeeded()
        
    }
    
    //根据图片张数设置布局,因此上去要去拿到图片数量
    private func calculatePictureViewSize(count:Int)->(pSize:CGSize,itemSize:CGSize){
        if count == 0{
            return (CGSize(width: 1, height: 1),CGSize(width: 1, height: 1))
        }
        //一张图片的时候
        if count == 1{
            //按图片原尺寸
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewModel?.imgUrls?.last?.wap360 ?? ""){
                let imgSize = image.size
                
                return (imgSize,imgSize)
            }
            
            return (CGSize(width: itemWidth, height: itemWidth),CGSize(width: itemWidth, height: itemWidth))
        }
        
        
        
        if count == 4{
            let w = itemWidth*2 + margin
            return (CGSize(width: w, height: w),CGSize(width: w, height: w))
        }
        //多张图片,根据行数确定
        /*
         1,2,3
         4,5,6
         7,8,9
         */
        //行数
        let row = CGFloat((count-1)/3) + 1
        let h = row*itemWidth + (row-1)*margin
        return (CGSize(width: maxWidth, height: h),CGSize(width: maxWidth, height: h))
    }

    
}
